package com.wedata.抖音;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.disdr.utils.DateUtils;
import com.wedata.HttpUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class 抖音来客后台 {


    public static void main(String[] args) {
        //申请下载();
       Date start = new Date(1752543515000L);
        Date end = new Date(1752485529000L);
        System.out.println(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS ,start));
        System.out.println(DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS,end));
    }

    private static void 申请下载(){
        String url = "https://life.douyin.com/life/trade_view/v1/sale/download/certificate_list/?industry=industry_common&root_life_account_id=7011378472920713250";
        /**
         * {
         *     "BaseResp": {
         *         "StatusCode": 103310533,
         *         "StatusMessage": "最多仅支持30个待下载文件，超过后请将文件下载完成后再生成新的文件"
         *     },
         *     "log_id": "20250714151801AF6A479A810D82ED9131",
         *     "now": "1752477482087",
         *     "status_code": 103310533,
         *     "status_msg": "最多仅支持30个待下载文件，超过后请将文件下载完成后再生成新的文件"
         * }
         */

        JSONObject body = new JSONObject();
        body.set("industry","industry_common");
        body.set("is_user_poi_filter",false);
        body.set("from_app",false);
        JSONObject filter = new JSONObject();
        filter.set("is_market",false);
        filter.set("is_market_poi",false);
        filter.set("start_time", 1752336000);//2025-07-13 00:00:00
        filter.set("end_time", 1752422399);//2025-07-13 23:59:59
        body.set("filter",filter);
        HttpUtils.sendPost(url, getHeader(), body);
        //sendRequest result:{"BaseResp":{"StatusCode":0,"StatusMessage":""},"data":{"download":{"download_id":"7526849418587277339","download_type":20,"file_name":"售卖明细_2025-07-13_2025-07-13.xlsx","file_url":"","generate_complete_time":0,"generate_start_time":1752481214,"poll_time":3,"status":1}},"log_id":"20250714162014C514400DB2DF59B71282","now":"1752481214808","status_code":0,"status_msg":""}
    }

    private static String 下载列表(String fileName, int num){
        if(fileName==null|| fileName.isEmpty()){
            fileName = "售卖明细_2025-07-13_2025-07-13.xlsx";
        }
        if(num>6){
            throw new RuntimeException("下载失败,等了一分钟都没下载成功");
        }
        //返回三十条，前端分页的，按生成时间倒序
        String url = "https://life.douyin.com/life/settle/v1/download/list/?download_type=20&root_life_account_id=7011378472920713250";
        Map<String, String> header = new HashMap<>();
        header.put("cookie", getHeader().getStr("cookie"));
        JSONObject result = HttpUtils.doGet(url, header);
        JSONArray download_infos = result.getJSONArray("download_infos");
        for (int i = 0; i < download_infos.size(); i++) {
            JSONObject download_info = download_infos.getJSONObject(i);
            String file_name = download_info.getStr("file_name");//售卖明细_2025-07-13_2025-07-13.xlsx
            String download_id = download_info.getStr("download_id");
            Integer status = download_info.getInt("status");
            if(fileName.equals(download_id) || fileName.equals(file_name)){
                if(status!=2){
                    try{
                        Thread.sleep(10000);
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                    return 下载列表(fileName, num+1);
                }else {
                    return download_info.getStr("file_url");
                }
            }
        }
        try{
            Thread.sleep(10000);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        return 下载列表(fileName, num+1);
    }
    private static void 下载(String url){
        //String url = "https://life.douyin.com/life/trade_view/v1/sale/download/certificate_list/?industry=industry_common&root_life_account_id=7011378472920713250";
    }

    private static JSONObject getHeader(){
        JSONObject header = new JSONObject();
        header.set("cookie", "passport_csrf_token=8d77eb1ee3aedc195fa373d9e9f69f1e; passport_csrf_token_default=8d77eb1ee3aedc195fa373d9e9f69f1e; __security_mc_1_s_sdk_crypt_sdk=89fce530-430b-876e; bd_ticket_guard_client_web_domain=2; passport_mfa_token=CjYjBEPwq7ImfHkCNsG4LfRnAkfs2HIb6txNnXy4LnzHI2LmBHd10jM2fZG1LP%2FuOSYkE3kxLpgaSgo8AAAAAAAAAAAAAE8cmB56xP%2FCd%2FcQMk2%2BGdqEfYGAKj3WW6xqvObsjBB4OTm4XquW9MMQvHlFa23wFE5DEP378w0Y9rHRbCACIgEDR6tuyQ%3D%3D; d_ticket=567042f24dfb504045d9fefb4ea78a5d86365; passport_assist_user=CkClKf2QL7tdkMVtwFsvb5CR6a6bLHf-N9IUgo8XvzN-khtkFUabZU02eGgNrJUu0kX981qtUd7cYpyT-jjJJT_fGkoKPAAAAAAAAAAAAABPHNrLK9G-crO6etQXhnHdoEEwn8R5k5dhCZdYt4Bkn4xK03Mf_b9lUxDKAqXRkC-EWhD8-_MNGImv1lQgASIBA1gx_oc%3D; n_mh=wF2e846kt0imJDrh1Shj7h64KzRX-MlIn1gXc1CA8tU; sid_guard=40b6433f88b5a72b328c4a5045b511fc%7C1749783133%7C5184000%7CTue%2C+12-Aug-2025+02%3A52%3A13+GMT; uid_tt=5e6a00a6989eaa3232b9d5195bfb2cd2; uid_tt_ss=5e6a00a6989eaa3232b9d5195bfb2cd2; sid_tt=40b6433f88b5a72b328c4a5045b511fc; sessionid=40b6433f88b5a72b328c4a5045b511fc; sessionid_ss=40b6433f88b5a72b328c4a5045b511fc; is_staff_user=false; sid_ucp_v1=1.0.0-KDllZjg0NzY0YjVmOGI4MjJhYjk0MjllYjUzZDAzNTg3MDA2ZWMwOTIKIAjelYGUy_VhEN2krsIGGNoWIAww_9Gs9gU4B0D0B0gEGgJsZiIgNDBiNjQzM2Y4OGI1YTcyYjMyOGM0YTUwNDViNTExZmM; ssid_ucp_v1=1.0.0-KDllZjg0NzY0YjVmOGI4MjJhYjk0MjllYjUzZDAzNTg3MDA2ZWMwOTIKIAjelYGUy_VhEN2krsIGGNoWIAww_9Gs9gU4B0D0B0gEGgJsZiIgNDBiNjQzM2Y4OGI1YTcyYjMyOGM0YTUwNDViNTExZmM; _bd_ticket_crypt_cookie=e2b5db4f39a41d93b3247b8542ddc979; __security_mc_1_s_sdk_sign_data_key_web_protect=53a6e5ec-403c-967b; __security_mc_1_s_sdk_cert_key=7dd62033-4d25-9151; __security_server_data_status=1; enter_pc_once=1; UIFID_TEMP=26198ff38959f773c63a6fc9b3542e2fdcfd2f10d2782124ed1adc24709862df0287100ed99b0a00a0b0994a9f31a2ac7d35284f4bd67b8a063dea6ce24c260ce4a4fb2b69b76463ac9c91f2204b9f31; SelfTabRedDotControl=%5B%5D; volume_info=%7B%22volume%22%3A0.6%2C%22isMute%22%3Afalse%7D; my_rd=2; gf_part_3175267=87; gfkadpd=299467,22075; csrf_session_id=026411b77d4e82a5bc51aeb47036040d; passport_auth_status_ls=a4db58f54f77e749dfba145a42515d68%2C; passport_auth_status_ss_ls=a4db58f54f77e749dfba145a42515d68%2C; sid_guard_ls=5491d89fdf33cf3505fd0ba1e36d9037%7C1752476124%7C5184002%7CFri%2C+12-Sep-2025+06%3A55%3A26+GMT; sid_guard_ls=5491d89fdf33cf3505fd0ba1e36d9037%7C1752476124%7C5184002%7CFri%2C+12-Sep-2025+06%3A55%3A26+GMT; is_hit_partitioned_cookie_canary=true; is_hit_partitioned_cookie_canary=true; uid_tt_ls=3fe48bc49a7168096a8977a569b16a35; uid_tt_ls=3fe48bc49a7168096a8977a569b16a35; uid_tt_ss_ls=3fe48bc49a7168096a8977a569b16a35; uid_tt_ss_ls=3fe48bc49a7168096a8977a569b16a35; is_hit_partitioned_cookie_canary_ss=true; is_hit_partitioned_cookie_canary_ss=true; sid_tt_ls=5491d89fdf33cf3505fd0ba1e36d9037; sid_tt_ls=5491d89fdf33cf3505fd0ba1e36d9037; sessionid_ls=5491d89fdf33cf3505fd0ba1e36d9037; sessionid_ls=5491d89fdf33cf3505fd0ba1e36d9037; sessionid_ss_ls=5491d89fdf33cf3505fd0ba1e36d9037; sessionid_ss_ls=5491d89fdf33cf3505fd0ba1e36d9037; session_tlb_tag_ls=sttt%7C6%7CVJHYn98zzzUF_Quh422QN_________-1H8uzYifr9dG1NsVJkg-6Dy3TyuOl4l5Cuq9pQkabQfc%3D; session_tlb_tag_ls=sttt%7C6%7CVJHYn98zzzUF_Quh422QN_________-1H8uzYifr9dG1NsVJkg-6Dy3TyuOl4l5Cuq9pQkabQfc%3D; is_staff_user_ls=false; is_staff_user_ls=false; sid_ucp_v1_ls=1.0.0-KDIyNGIwNDZkN2JlMDJkNmJhZjM4M2MxNWQ1MzhjYTczOWE5NDBmNmYKHAiut_DdnIz-AhDc09LDBhjRwRIgDDgCQPEHSAQaAmhsIiA1NDkxZDg5ZmRmMzNjZjM1MDVmZDBiYTFlMzZkOTAzNw; sid_ucp_v1_ls=1.0.0-KDIyNGIwNDZkN2JlMDJkNmJhZjM4M2MxNWQ1MzhjYTczOWE5NDBmNmYKHAiut_DdnIz-AhDc09LDBhjRwRIgDDgCQPEHSAQaAmhsIiA1NDkxZDg5ZmRmMzNjZjM1MDVmZDBiYTFlMzZkOTAzNw; ssid_ucp_v1_ls=1.0.0-KDIyNGIwNDZkN2JlMDJkNmJhZjM4M2MxNWQ1MzhjYTczOWE5NDBmNmYKHAiut_DdnIz-AhDc09LDBhjRwRIgDDgCQPEHSAQaAmhsIiA1NDkxZDg5ZmRmMzNjZjM1MDVmZDBiYTFlMzZkOTAzNw; ssid_ucp_v1_ls=1.0.0-KDIyNGIwNDZkN2JlMDJkNmJhZjM4M2MxNWQ1MzhjYTczOWE5NDBmNmYKHAiut_DdnIz-AhDc09LDBhjRwRIgDDgCQPEHSAQaAmhsIiA1NDkxZDg5ZmRmMzNjZjM1MDVmZDBiYTFlMzZkOTAzNw; odin_tt=db5fcd0391a2547a3b4789c634bd9c299a24979468dd612ed58962ec8bc892a00ff2fa0dfb50c791a8ad2db361847c3b; _bd_ticket_crypt_doamin=3; ttwid=1%7CbKRy6LeepZGoaX0StlOOCulBfD5sewb42SbURDrPVDM%7C1752476353%7Cf154bc6726bcc7fd07edc22d3d05b2f660a5f02b98b8171b9d76dd50440015fc; bd_ticket_guard_client_data=eyJiZC10aWNrZXQtZ3VhcmQtdmVyc2lvbiI6MiwiYmQtdGlja2V0LWd1YXJkLWl0ZXJhdGlvbi12ZXJzaW9uIjoxLCJiZC10aWNrZXQtZ3VhcmQtcmVlLXB1YmxpYy1rZXkiOiJCUEtTUmlWZUo0WUZpKzY0a1FISFNpM28xZnNpTU12a2JaVHN1N3VnUmdVNk5jYVBxUGU1S3BQZHNEQjNHLzduVFV0Tm1QOHNHcUN3T2hKRk8wRm9USTQ9IiwiYmQtdGlja2V0LWd1YXJkLXdlYi12ZXJzaW9uIjoxfQ%3D%3D");
        return header;
    }
}
